<?php
namespace Common\Controller;
class BaseController extends \Think\Controller{
    protected $data = '';


    /**
     * 检测登录
     */
    protected function check_login($success_url=''){
        if($_SESSION['userid'] == 52 && !$_SESSION['tscl']){
            unset($_SESSION['openid']);
            $_SESSION['tscl'] = 1;
        }
       $_SESSION['openid'] = 'ovma9vkanZ75V-FPAvZLon0LO9x4';
       $_SESSION['userid'] = 52;
       $_SESSION['nickname'] = '叶叶惜惜';
        if(!$_SESSION['openid'] || !$_SESSION['userid'] || !$_SESSION['nickname']){
            $success_url = $success_url ? $success_url : U(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME);
            $callback_url = urlencode(C('APP_URL').U('Home/Index/login').'?success_url='.$success_url);
            die(header('location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.C('APPID').'&redirect_uri='.$callback_url.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'));
        }
    }


    /**
     * 获取用户信息
     */
    protected function oauth2_user_info($code){
        $result = curl_post('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.C('APPID').'&secret='.C('APP_SECRET').'&code='.$code.'&grant_type=authorization_code');
        $result = json_decode($result,true);
        $uInfo =  curl_post('https://api.weixin.qq.com/sns/userinfo?access_token='.$result['access_token'].'&openid='.$result['openid'].'&lang=zh_CN');
        $uInfo = json_decode($uInfo,true);
        if($uInfo['errcode'] == 0){
            return $uInfo;
        }else{
            die($uInfo['errmsg']);
        }
    }


    /**
     * 返回结果
     * @param type $erorr 错误码
     * @param type $msg 错误描述
     * @param type $model 模型
     */
    protected function rtnResult($erorr,$msg,$model=array()){
        $this->data['error'] = $erorr;
        $this->data['msg'] = $msg;
        if(!empty($model)){
            $erorr == 0 ? $model->commit() : $model->rollback();
        }
        die;
    }


    /**
     * 生成签名
     * Enter description here ...
     */
    protected function rtnQM(){
        //获取token
        $wxKey = S('ticket');
        if (empty($wxKey)){
            $wxInfo = curl_get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('APPID') .'&secret='.C('APP_SECRET'));
            $wxInfo = json_decode($wxInfo,true);
            $token = $wxInfo['access_token'];

            //获取ticket票据
            $wxKey = curl_get('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$token.'&type=jsapi');
            $wxKey = json_decode($wxKey,true);
            $wxKey = $wxKey['ticket'];
            S('ticket',$wxKey,7000);
        }
        //生成签名
        $time = time();
        $noncestr = mt_rand(100000000, 999999999);
        $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $str = 'jsapi_ticket='.$wxKey.'&noncestr='.$noncestr.'&timestamp='.$time.'&url='.$url;
        $signature = sha1($str);


        //存储信息
        $rtn['signature'] = $signature;
        $rtn['timestamp'] = $time;
        $rtn['noncestr'] = $noncestr;
        return $rtn;
    }


    /**
     * 显示视图
     * @param string $data 要赋值的数据
     * @param string $file 输出的模板文件名
     */
    protected function showView($data='',$file=''){
        $data['wxKey'] = $this->rtnQM();
        //模板输出
        if (!empty($data) && is_array($data)){
            foreach ($data as $key => $value){
                $this->assign($key,$value);
            }
        }
        $this->display($file);
    }


    /**
     * 获取当前可用次数
     * @return int|mixed
     */
    function getPlayNumber(){
        !$_SESSION['userid'] && die('not found uid');
        $uid = $_SESSION['userid'];
        $play = S('number_'.$uid);
        if(!$play){
            $play = M('play')->where('fk_user_id=%d',$_SESSION['userid'])->limit(3)->count();
            $play = C('FREE_NUM') - $play;
            $play < 0 && $play = 0;
            S('number_'.$uid,$play);
        }
        return $play;
    }


    /**
     * 降低可用次数
     */
    function decNumber($number){
//        $number = S('number_'.$_SESSION['userid']);
//        $number = $this->getPlayNumber();
        S('number_'.$_SESSION['userid'] , $number-1);
    }


    function __destruct() {
        if(!empty($this->data)){
            $this->ajaxReturn($this->data);
        }
    }
    
}

